"""
@File    :   out.py
@Time    :   2024/03/25 08:35:04
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   全局的pydantic序列化模型
"""

from functools import lru_cache
from typing import Any, Type

from pydantic import BaseModel, ConfigDict

from src.models.models import OrmBase


class BaseOutSchemaConfigMixin:
    """全局Pydantic序列化模型的配置混合类"""

    model_config = ConfigDict(
        populate_by_name=True,  # 是否允许使用别名进行实例化(默认False)
        use_enum_values=True,  # 直接将枚举类型转为value
    )

    @classmethod
    @lru_cache
    def convert_to_sql_model_field(
        cls: Type[BaseModel],
        sql_model: Type[OrmBase],
        exclude_field: tuple | None = None,
    ) -> list[Any]:
        """将pydantic序列化模型字段转为sql模型字段

        Args:
            cls (Type[BaseModel]): pydantic模型
            sql_model (Type[OrmBase]): sqlalchemy模型
            exclude_field (list[str] | None, optional): 需要排除的字段

        Returns:
            list[Any]: sql模型字段列表
        """
        if exclude_field is None:
            exclude_field = ()
        return [
            getattr(sql_model, i)
            for i in cls.model_fields.keys()
            if i not in exclude_field
        ]
